const {addUserDao, getUserListDao, getUserByIdDao} = require("../dao/userDao");
const {formatResponse} = require("../utils/tool");
const validate = require("validate.js");
const moment = require("moment");

// 关闭moment必须要使用的格式警告
moment.suppressDeprecationWarnings = true;

// 使用 datetime 必须添加 datetime 校验规则
validate.extend(validate.validators.datetime, {
    parse: function(value, options) {
        return moment.utc(value, options.format).valueOf();
    },
    format: function(value, options) {
        // 用户显示错误消息时，需要显示信息的日期格式
        let format = options.dateOnly ? "YYYY-MM-DD" : "YYYY-MM-DD hh:mm:ss";
        return moment.utc(value).format(format);
    }
});

// 添加文章
module.exports.addUserService = async (userInfo) => {
    // 定义校验规则
    const userRule = {
        code: {
            presence: {
                allowEmpty: false
            },
            type: "string",
        },
        password: {
            presence: {
                allowEmpty: false
            },
            type: "string",
        },
        name: {
            presence: {
                allowEmpty: false
            },
            type: "string",
        },
        phone: {
            presence: {
                allowEmpty: true
            },
            type: "string",
        },
        email: {
            presence: {
                allowEmpty: true
            },
            type: "string",
        },
        avatar: {
            presence: {
                allowEmpty: true
            },
            type: "string",
        },
        gender: {
            presence: {
                allowEmpty: true
            },
            type: "integer",
            inclusion: [0, 1]
        },
        graduationYear: {
            presence: {
                allowEmpty: true
            },
            datetime: {
                dateOnly: true, // 只需要日期，不需要具体时间
            }
        },
        idNumber: {
            presence: {
                allowEmpty: false
            },
            type: "string",
        },
        role: {
            presence: {
                allowEmpty: true
            },
            type: "integer",
            inclusion: [0, 1, 2]
        },
        major: {
            presence: {
                allowEmpty: true
            },
            type: "string",
        },
        // 身份证件
        idCardUrl: {
            presence: {
                allowEmpty: true
            },
            type: "string",
        },
        // 学籍证件
        studentCardUrl: {
            presence: {
                allowEmpty: true
            },
            type: "string",
        },
        // 是否为学生
        isStudent: {
            presence: {
                allowEmpty: false
            },
            type: "integer",
        }
    }
    try {
        await validate.async(userInfo, userRule);
        const result = await addUserDao(userInfo)
        return formatResponse(0, '添加成功', result);
    } catch (error) {
        return formatResponse(1, '数据校验错误', error);
    }
}
// 分页获取用户
module.exports.getUserListService = async (page, limit) => {
    const result = await getUserListDao(page, limit);
    return formatResponse(0, '查询成功', result);
}
// 根据 id 获取单个用户信息
module.exports.getUserByIdService = async (id) => {
    const result = await getUserByIdDao(id);
    return formatResponse(0, '查询成功', result);
}